package com;
import java.util.Random;

public class GameMap {
	private boolean[][] map; //地图二维数组
	private int rows;
	private int cols;
	
	GameMap () {
		rows = cols = 20;
		map = new boolean[rows][cols];//地图二维数组
		
	}
	
	GameMap (int r, int c, boolean[][] t) {
    rows = r;
		cols = c;
		map = new boolean[rows][cols];//地图二维数组
		
		
		for(int i = 0; i<rows; i++){
			for(int j = 0; j<cols; j++){
				map[i][j] = t[i][j];
			}
		}
	}
	
	public boolean reset (int life_ratio) {
       
        if(rows<0 || cols <0){
            return false;
        }

        int i,j;
        Random random=new Random(life_ratio);
        for(i=0;i<rows;i++) {
            for(j=0;j<cols;j++) {
                if (random.nextInt(2) % 2 == 0) { //[0,2)生成一个随机数
                    map[i][j] = true;
                }else{
                    map[i][j] = false;
                }
            }
        }
        return true;
    }

    public int getNeighborCount (int row,int col) {
        int count = 0;
        for(int i =row-1;i<=row+1;i++){
            for(int j = col-1;j<=col+1;j++){
                //棋盘外的情况
                if (i < 0 || j < 0 || i >= rows || j >= cols || (i == row && j == col)) {
                    continue;
                }
                if (map[i][j] == true) {
                    count=count+1;
                }
            }
        }
        return count;
    }

    public void set (int row,int col, boolean val) {
    	map[row][col] = val;
    }

    public boolean get(int row,int col){
        return map[row][col];
    }
    
    public int getRows(){
    	return rows;
    }
    
    public int getCols(){
    	return cols;
    }
    
    public boolean[][] getMap(){
    	return map;
    }
    
    public void output_map() {
        int i, j;

        for (i = 0; i < rows; i++) {
            for (j = 0; j < cols; j++) {
                System.out.print(map[i][j]+" ");
            }
            System.out.print('\n');
        }
    }
}
	